package org.noote.test_virtualpad;

import java.text.DecimalFormat;

import org.noote.libs.virtualpad.Virtual1DPad;
import org.noote.libs.virtualpad.Virtual5DPad;
import org.noote.libs.virtualpad.VirtualPad_Input;
import org.noote.libs.virtualpad.VirtualTouchPad;
import org.noote.libs.virtualpad.VirtualTouchPad_Renderer;
import org.noote.libs.virtualpad.VirtualVerticalSlider;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class DisplayView extends View {
	
	Paint _paint, _paintFont, _paintMessageFont;
	Bitmap _bitmapVTarget;
	
	VirtualTouchPad_Renderer _padRenderer = new VirtualTouchPad_Renderer();
	VirtualPad_Input _padInput = new VirtualPad_Input();
	
	Virtual1DPad _padRadar = new Virtual1DPad();
	Virtual1DPad _padScanLine = new Virtual1DPad();
	Virtual1DPad _padHalf = new Virtual1DPad(true);
	Virtual1DPad _padQuick = new Virtual1DPad(true);
	Virtual5DPad _padBody = new Virtual5DPad();
	VirtualVerticalSlider _padBodySpeed = new VirtualVerticalSlider();
	VirtualTouchPad _padTurrel = new VirtualTouchPad();
	Virtual1DPad _padReinitTurrel = new Virtual1DPad();
	Virtual1DPad _padReport = new Virtual1DPad(true);
	
	String _sTargetMessage = "";
	boolean _bFrontLeftSwitch = false;
	boolean _bFrontRightSwitch = false;
	boolean _bBackLeftSwitch = false;
	boolean _bBackRightSwitch = false;
	int _iTurrelLow = 0;
	int _iTurrelHigh = 0;
	int _iTurrelDistance = 38;
	boolean _bOnline = false;
	boolean _bReportTransmission = false;

	static DecimalFormat m_dfDistance = new DecimalFormat("#,##0.00");
	
	public DisplayView(Context context) {
        super(context);
        
        _paint = new Paint();
        _paint.setColor(Color.WHITE);
        _paint.setStyle(Paint.Style.STROKE);
        
        _paintFont = new Paint();
        _paintFont.setColor(Color.WHITE);
        _paintFont.setStyle(Paint.Style.STROKE);
        _paintFont.setTextSize(20);
        _paintFont.setShadowLayer(2, 0, 0, Color.DKGRAY);        

        _paintMessageFont = new Paint();
        _paintMessageFont.setColor(Color.RED);
        _paintMessageFont.setTextSize(32);
        _paintMessageFont.setStyle(Paint.Style.STROKE);
        _paintMessageFont.setShadowLayer(3, 0, 0, Color.WHITE);            
	}
	
	@Override
	protected void onSizeChanged(int width, int height, int width_old, int height_old)
	{
        float fScale = 0.75f;
		if(width>(255*2.5f)) fScale = 1.0f;
        else if(width<255) fScale = 0.5f;
		
       	_bitmapVTarget = _load_image(R.drawable.vtarget_transparent, fScale);
       	
		_padRenderer.loadImages(getContext(), fScale);       	

		_padScanLine.setWorkSpace(0, 0, _padRenderer.getWidth(_padScanLine), _padRenderer.getHeight(_padScanLine));
		_padScanLine.setText("ScLn");
		_padInput.add(_padScanLine);
		
		_padRadar.setWorkSpace(_padRenderer.getWidth(_padScanLine), 0, _padRenderer.getWidth(_padRadar), _padRenderer.getHeight(_padRadar));
		_padRadar.setText("Rdr");
		_padInput.add(_padRadar);
		
		_padHalf.setWorkSpace(0, _padRenderer.getHeight(_padScanLine)+_padRenderer.getHeight(_padScanLine)/2, _padRenderer.getWidth(_padHalf), _padRenderer.getHeight(_padHalf));
		_padHalf.setText("1/2");
		_padHalf.setLock(true);
		_padInput.add(_padHalf);
		
		_padQuick.setWorkSpace(0, _padRenderer.getHeight(_padScanLine)*2+_padRenderer.getHeight(_padScanLine)/2, _padRenderer.getWidth(_padQuick), _padRenderer.getHeight(_padQuick));
		_padQuick.setText("Qik");
		_padQuick.setLock(true);
		_padInput.add(_padQuick);
        
		_padReport.setWorkSpace(width-_padRenderer.getWidth(_padReport), 0, _padRenderer.getWidth(_padReport), _padRenderer.getHeight(_padReport));
		_padReport.setText("Tx");
        _padInput.add(_padReport);
		
        _padBody.setWorkSpace(0, height-_padRenderer.getHeight(_padBody), _padRenderer.getWidth(_padBody), _padRenderer.getHeight(_padBody));
		_padInput.add(_padBody);
		
		_padBodySpeed.setWorkSpace(_padRenderer.getHeight(_padBody), height-_padRenderer.getHeight(_padBodySpeed), _padRenderer.getWidth(_padBodySpeed), _padRenderer.getHeight(_padBodySpeed));
		_padInput.add(_padBodySpeed);
        
		_padTurrel.setWorkSpace(width-_padRenderer.getWidth(_padTurrel), height-_padRenderer.getHeight(_padTurrel), _padRenderer.getWidth(_padTurrel), _padRenderer.getHeight(_padTurrel));
        _padInput.add(_padTurrel);
        
        _padReinitTurrel.setWorkSpace(width-_padRenderer.getWidth(_padReinitTurrel), height-_padRenderer.getHeight(_padTurrel)-_padRenderer.getHeight(_padReinitTurrel), _padRenderer.getWidth(_padReinitTurrel), _padRenderer.getHeight(_padReinitTurrel));
        _padReinitTurrel.setText("Rst");
        _padInput.add(_padReinitTurrel);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		
		_padBodySpeed.setText(String.format("%3.0f %%", (1.0f-_padBodySpeed.getPosition())*100));
		_padRenderer.draw(canvas, _padInput);
		
		if(_bitmapVTarget != null)
		{
			canvas.drawBitmap(_bitmapVTarget, (getWidth()-_bitmapVTarget.getWidth())/2.0f, (getHeight()-_bitmapVTarget.getHeight())/2.0f, null);

			if(_sTargetMessage.length()>0)
			{
				Rect bounds = new Rect();
				_paintMessageFont.getTextBounds(_sTargetMessage, 0, _sTargetMessage.length(), bounds);
				canvas.drawText(_sTargetMessage, (getWidth()-bounds.width())/2.0f, (getHeight()-_bitmapVTarget.getHeight())/2.0f-bounds.height(), _paintMessageFont);
			}
			
			// turrel distance
			{
				String sTurrelDistance;
				if(_iTurrelDistance<25) // too low
					sTurrelDistance = ".";
				else if(_iTurrelDistance>145) // too big
					sTurrelDistance = "*";
				else
					sTurrelDistance = "" + _iTurrelDistance + " cm";

				Rect bounds = new Rect();
				_paintFont.getTextBounds(sTurrelDistance, 0, sTurrelDistance.length(), bounds);
				canvas.drawText(sTurrelDistance, (getWidth()-bounds.width())/2.0f, (getHeight()+_bitmapVTarget.getHeight())/2.0f+bounds.height(), _paintFont);
			}
		}
	}
	
	public void setTargetMessag(String sMessage)
	{
		_sTargetMessage = sMessage;
		invalidate();
	}
	
	private Bitmap _load_image(int idRessource, float fScale)
	{
		final Bitmap loadedBitmap = BitmapFactory.decodeStream(getContext().getResources().openRawResource(idRessource));
		if(loadedBitmap != null)
			return Bitmap.createScaledBitmap(loadedBitmap, (int)(loadedBitmap.getWidth()*fScale), (int)(loadedBitmap.getHeight()*fScale), true);
		
		return null;
	}
}
